import math
import random
import time
import pywasm
import requests

headers = {
    'accept': 'application/json, text/javascript, */*; q=0.01',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    'pragma': 'no-cache',
    'referer': 'https://match.yuanrenxue.com/match/15',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'x-requested-with': 'XMLHttpRequest',
    "User-Agent": "yuanrenxue.project",
    "Cookie": "sessionid=pwin2ge9j7t9ttj5m15pjwar9m4sj9tc;",
}


# 主要难点是 py 执行 wasm 文件
def get_data(page):
    t = int(time.time())
    t1 = int(t / 2)
    t2 = int(t / 2 - math.floor(random.random() * 50 + 1))
    vm = pywasm.load('./main.wasm')
    sign = vm.exec("encode", [t1, t2])
    m = f'{sign}|{t1}|{t2}'
    url = 'https://match.yuanrenxue.com/api/match/15?page={}&m={}'.format(page, m)
    return requests.get(url, headers=headers).json()


def main():
    value_list = []
    for page in range(1, 6):
        res = get_data(page)
        print("res==>", res)
        value_list += [v['value'] for v in res['data']]
    print(value_list)
    print(sum(value_list))


if __name__ == '__main__':
    main()
